package com.db.compare.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.db.compare.core.BaseResponse;
import com.db.compare.model.RbacAuth;
import com.db.compare.mapper.RbacAuthMapper;
import com.db.compare.service.RbacAuthService;
import com.db.compare.vo.RbacAuthVo;
import com.db.compare.exp.RbacAuthExp;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.db.compare.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import com.alibaba.fastjson.JSONObject;


/**
* 权限表
* */
@Slf4j
@Controller
@RequestMapping("/rbac/auth")
public class RbacAuthController extends BaseController{

    @Autowired
    private RbacAuthMapper rbacauthMapper;
    @Autowired
    private RbacAuthService rbacauthService;

    @RequestMapping(value={"/list",""})
    public String rbacauthList(){
        return "/html/RbacAuth";
    }

    @RequestMapping("/pages")
    @ResponseBody
    public PageInfo<RbacAuth> pages(Model model,@RequestParam Map rbacauthMap){
        RbacAuthVo rbacauthVo = JSON.parseObject(JSON.toJSONString(rbacauthMap), RbacAuthVo.class);
        return rbacauthService.pageInfo(rbacauthVo);
    }

    @RequestMapping("/del")
    @ResponseBody
    public BaseResponse del(@RequestParam String ids){
        System.out.println(ids);
        String[] arrays = ids.split(",");
        rbacauthMapper.deleteByBatch(arrays);
        return new BaseResponse();
    }

    @RequestMapping("/add")
    @ResponseBody
    public BaseResponse add(@RequestBody RbacAuth rbacauth){
        rbacauth.setUpdateTime(new Date());
        if(rbacauth.getId()==null) {
            rbacauth.setCreateTime(new Date());
            rbacauthMapper.insert(rbacauth);
        }else{
            rbacauthMapper.updateByPrimaryKey(rbacauth);
        }
        return new BaseResponse(200,rbacauth);
    }
    @RequestMapping("/get")
    @ResponseBody
    public BaseResponse get(@RequestParam String id){
        int pk=Integer.parseInt(id);
        RbacAuth rbacauth = rbacauthMapper.selectByPrimaryKey(pk);
        return new BaseResponse(200,rbacauth);
    }
    //下载模板
    @RequestMapping("/download")
    public void testRespExcel(HttpServletRequest request,HttpServletResponse response)throws Exception{
        String ids=request.getParameter("ids");
        rbacauthService.download(ids,response);
    }

    @RequestMapping("/upload")
    @ResponseBody
    public BaseResponse upload(@RequestParam("file") MultipartFile file)throws Exception{
        if(!rbacauthService.upload(file)){
            return buildFailResponse();
        }
        return buildSuccessResponse();
    }
}